﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ManagerList : System.Web.UI.Page
{
    protected DataTable _roleTable;
    protected void Bind()
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("ManagerId,ManagerName,Password,PenName,AddTime,UpTime,RoleId,LoginUrl", "manager", "", "AddTime DESC");
        this._roleTable = db.DataTable("RoleId,[Title]", "managerRole", string.Format("SiteLanguage={0}", SiteInfo.SiteLanguage()), "");
        db.Close();
        this.ManagerList.DataSource = table;
        this.ManagerList.DataBind();
    }

    protected void ManagerList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ManagerList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void ManagerList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.ManagerList.EditIndex = -1;
        this.Bind();
    }

    protected void ManagerList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Button button = (Button)e.Row.FindControl("EditBt");
        Button button2 = (Button)e.Row.FindControl("DelBt");
        if (button2 != null)
        {
            if (!ManagerLogin.ValidateRole("C_ManagerList.aspx_b"))
            {
                button.Enabled = false;
            }
            if (!ManagerLogin.ValidateRole("C_ManagerList.aspx_a"))
            {
                button2.Enabled = false;
            }
        }
        CheckBoxList list = (CheckBoxList)e.Row.FindControl("ManagerRoleIdList");
        if (list != null)
        {
            list.DataSource = this._roleTable;
            list.DataValueField = "RoleId";
            list.DataTextField = "Title";
            list.DataBind();
            string str = ((HiddenField)e.Row.FindControl("RoleIdHiddenField")).Value;
            for (int i = 0; i < list.Items.Count; i++)
            {
                string str2 = list.Items[i].Value;
                if (str.Contains("," + str2 + ","))
                {
                    list.Items[i].Selected = true;
                }
            }
        }
    }

    protected void ManagerList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.ManagerList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Delete("manager", string.Format("ManagerId={0}", str));
        db.Close();
        this.Bind();
        this.Msg.Text = "管理员删除成功！";
        this.Msg.Visible = true;
    }

    protected void ManagerList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.ManagerList.EditIndex = e.NewEditIndex;
        this.Bind();
    }

    protected void ManagerList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (base.IsValid)
        {
            string[,] strArray;
            int rowIndex = e.RowIndex;
            string str = this.ManagerList.DataKeys[rowIndex].Values[0].ToString();
            string str2 = this.ManagerList.Rows[rowIndex].Cells[1].Text.ToString();
            string str3 = ((TextBox)this.ManagerList.Rows[rowIndex].FindControl("ChangPassword")).Text.ToString();
            string str4 = Function.MD5(str3);
            string str5 = ((TextBox)this.ManagerList.Rows[rowIndex].Cells[3].Controls[0]).Text.ToString();
            string str6 = ((TextBox)this.ManagerList.Rows[rowIndex].Cells[5].Controls[0]).Text.ToString();
            CheckBoxList list = (CheckBoxList)this.ManagerList.Rows[rowIndex].FindControl("ManagerRoleIdList");
            string str7 = ",";
            for (int i = 0; i < list.Items.Count; i++)
            {
                if (list.Items[i].Selected)
                {
                    str7 = string.Format("{1}{0},", list.Items[i].Value, str7);
                }
            }
            if (str3 == "")
            {
                strArray = new string[,] { { "PenName", str5, "NVarChar", "32" }, { "UpTime", DateTime.Now.ToString(), "dateTime", "0" }, { "RoleId", str7, "NVarChar", "0" }, { "loginUrl", str6, "NVarChar", "0" } };
            }
            else
            {
                strArray = new string[,] { { "Password", str4, "NVarChar", "32" }, { "PenName", str5, "NVarChar", "32" }, { "UpTime", DateTime.Now.ToString(), "dateTime", "0" }, { "RoleId", str7, "NVarChar", "0" }, { "loginUrl", str6, "NVarChar", "0" } };
            }
            IDb db = new DbFactory().DriverDb();
            db.Open();
            db.Update("manager", strArray, string.Format("ManagerId={0}", str));
            db.Close();
            this.Msg.Text = string.Format("管理员 {0} 的信息修改成功!", str2);
            this.Msg.Visible = true;
        }
        this.ManagerList.EditIndex = -1;
        this.Bind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        base.MaintainScrollPositionOnPostBack = true;
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
}
